package sample;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Timer;
import java.util.TimerTask;

public class SwingControlDemo {
    private JFrame mainFrame;
    private JPanel controlPanel;
    private JButton okButton;
    private String[] users;
    private JLabel owner;
    private int time = 0;
    private int count;
    private int winerCount;
    private String[] winer;
    private JTextField jTextField;

    public SwingControlDemo() {
        users = new String[]{"222"};
        count = users.length;
        prepareGUI();
    }

    public static void main(String[] args) {
        SwingControlDemo swingControlDemo = new SwingControlDemo();
        swingControlDemo.showEventDemo();
    }

    private void prepareGUI() {
        mainFrame = new JFrame("Java SWING Examples");
        mainFrame.setSize(400, 400);
        mainFrame.setLayout(new GridLayout(3, 1));
        controlPanel = new JPanel();
        controlPanel.setLayout(new FlowLayout());
        //输入框
        jTextField = new JTextField("输入抽取人数", 10);
        jTextField.addMouseListener(new JTextFiledMouseClickListener(jTextField));
        controlPanel.add(jTextField);
        //确定按钮
        okButton = new JButton("开始");
        okButton.setActionCommand("OK");
        okButton.addActionListener(new ButtonClickListener());
        controlPanel.add(okButton);
        mainFrame.add(controlPanel);
        //动画文字显示框
        owner = new JLabel("输入抽取数量，点击开始");
        mainFrame.add(owner);
        mainFrame.setVisible(true);
        mainFrame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosed(WindowEvent e) {
                super.windowClosed(e);
                System.exit(0);
            }
        });
    }

    private void showEventDemo() {
    }

    private class ButtonClickListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            String command = e.getActionCommand();
            if (command.equals("OK")) {
                winerCount = Integer.parseInt(jTextField.getText());
                if (!isGoodInput()) {
                    owner.setText("输入的人员数目不合法");
                } else {
                    showAnimation();
                }
            }
        }
    }

    /**
     * 执行任务前的检查
     *
     * @return
     */
    private boolean isGoodInput() {
        //重名，人数
        if (winerCount > count) {
            System.out.println("go");
            return false;
        } else {
            return true;
        }
    }

    /**
     * 展示抽奖动画
     */
    private void showAnimation() {
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                int position = time % count;
                owner.setText(users[position]);
                time++;
            }
        };
        Timer timer = new Timer();
        timer.schedule(timerTask, 0, 100);
        TimerTask timerTask1 = new TimerTask() {
            @Override
            public void run() {
              //  showWinner();
                timer.cancel();
                owner.setText("获奖人员");
            }
        };
        Timer timer1 = new Timer();
        timer1.schedule(timerTask1,2000);
    }

    /**
     * 展示待抽奖人员
     */
    private void showUser() {

    }

    private class JTextFiledMouseClickListener implements MouseListener {
        JTextField jTextField;

        public JTextFiledMouseClickListener(JTextField jTextField) {
            this.jTextField = jTextField;
        }

        @Override
        public void mouseClicked(MouseEvent e) {
            jTextField.setText("");
        }

        @Override
        public void mousePressed(MouseEvent e) {

        }

        @Override
        public void mouseReleased(MouseEvent e) {

        }

        @Override
        public void mouseEntered(MouseEvent e) {

        }

        @Override
        public void mouseExited(MouseEvent e) {

        }
    }

}
